package com.example.thirdhomework

import android.content.Intent
import android.graphics.Bitmap
import android.os.Bundle
import android.provider.MediaStore
import android.widget.Button
import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.activity.result.ActivityResultCallback
import androidx.activity.result.contract.ActivityResultContract
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        val firstButton = findViewById<Button>(R.id.first_button)
        firstButton.setOnClickListener {
            val intent = Intent(this, FirstActivity::class.java)
            //获取输入框的值并传给第二个页面
            val inputText = findViewById<EditText>(R.id.input_text).text.toString()
            intent.putExtra("input_text", inputText)
            startActivity(intent)
        }
        val launcher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
            if (it.resultCode == RESULT_OK) {
                val result = it.data?.getStringExtra("result")
                val textView = findViewById<TextView>(R.id.textView)
                textView.text = result
            }
        }

        val secondButton = findViewById<Button>(R.id.second_button)
        secondButton.setOnClickListener {
            val intent = Intent(this, SecondActivity::class.java)
            launcher.launch(intent)
//            //1和2配合使用获取传递的数据
//            //过时的获取回调方法1
//            startActivityForResult(intent, 1)
        }
        val takephoto = findViewById<Button>(R.id.take_photo)
//        点击takephoto拍照并显示在imageView上
        takephoto.setOnClickListener {
            dispatchTakePictureIntent()
        }

        val thirdButton = findViewById<Button>(R.id.third_button)
        thirdButton.setOnClickListener {
            val intent = Intent(this, ThirdActivity::class.java)
            startActivity(intent)
        }

    }
    

    val IMAGE_CPATURE = 1

    private  fun dispatchTakePictureIntent() {
        val takePictureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
        if(intent.resolveActivity(packageManager) != null){
            startActivityForResult(takePictureIntent, IMAGE_CPATURE)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(requestCode == IMAGE_CPATURE && resultCode == RESULT_OK){
            val imageBitmap = data?.extras?.get("data") as Bitmap
            val imageView = findViewById<ImageView>(R.id.imageView)
            imageView.setImageBitmap(imageBitmap)
        }
    }

}